package yur.wedding.modules.sys.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


import com.alibaba.druid.util.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import yur.wedding.common.utils.Constant;
import yur.wedding.exception.RRException;
import yur.wedding.modules.sys.entity.SysDocumentDocsubcategoryEntity;
import yur.wedding.modules.sys.entity.SysDocumentEntity;
import yur.wedding.modules.sys.entity.SysMenuEntity;
import yur.wedding.modules.sys.service.SysDocumentDocsubcategoryService;

import yur.wedding.modules.sys.service.SysDocumentService;
import yur.wedding.utils.PageUtils;
import yur.wedding.utils.R;
import yur.wedding.validator.ValidatorUtils;

import javax.annotation.Resource;


/**
 * 文档目录管理
 *
 * @author Yur
 * @email ${email}
 * @date 2022-08-05 07:19:51
 */
@RestController
@RequestMapping("sys/sysdocumentdocsubcategory")
public class SysDocumentDocsubcategoryController {


    @Autowired
    private SysDocumentDocsubcategoryService sysDocumentDocsubcategoryService;

    @Autowired
    private  SysDocumentDirectoryrightsController sysDocumentDirectoryrightsController;

    @Autowired
    protected SysDocumentService sysDocumentService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:sysdocumentdocsubcategory:list")
    public List list(@RequestParam Map<String, Object> params) {
        List<SysDocumentDocsubcategoryEntity> list = sysDocumentDocsubcategoryService.list();
        //查询父类名称
        for (SysDocumentDocsubcategoryEntity entity : list) {
            SysDocumentDocsubcategoryEntity byId = sysDocumentDocsubcategoryService.getById(entity.getMaincategoryId());
            if (byId != null) {
                entity.setParentName(byId.getCategoryname());
            }

        }


        return list;
    }

    /**
     * 只查询不包括按钮  用于弹框 添加目录或者修改等
     *
     * @return
     */
    @RequestMapping("/select")
    @RequiresPermissions("sys:sysdocumentdocsubcategory:select")
    public R select() {
        String parentName = "%%";
        List<SysDocumentDocsubcategoryEntity> docsubcategoryList = sysDocumentDocsubcategoryService.queryNotButtonList(parentName);
        SysDocumentDocsubcategoryEntity entity = new SysDocumentDocsubcategoryEntity();
        entity.setDbgyId(0L);
        entity.setCategoryname("一级菜单");
        entity.setMaincategoryId(-1L);
        entity.setOpen(true);
        docsubcategoryList.add(entity);
        return R.ok().put("docsubcategoryList", docsubcategoryList);
    }

    /**
     * 模糊查找 目录
     *
     * @return
     */
    @RequestMapping("/selectLike")
    @RequiresPermissions("sys:sysdocumentdocsubcategory:selectLike")
    public R selectLike(@RequestBody String parentName) {

        List<SysDocumentDocsubcategoryEntity> docsubcategoryList = null;
        if (!"FAIL".equals(parentName)) {
            docsubcategoryList = sysDocumentDocsubcategoryService.queryNotButtonLikeList("%" + parentName + "%");
        } else {
            parentName = "%%";
            docsubcategoryList = sysDocumentDocsubcategoryService.queryNotButtonList(parentName);
        }
        SysDocumentDocsubcategoryEntity entity = new SysDocumentDocsubcategoryEntity();
        entity.setDbgyId(0L);
        entity.setCategoryname("一级菜单");
        entity.setMaincategoryId(-1L);
        entity.setOpen(true);
        docsubcategoryList.add(entity);
        return R.ok().put("docsubcategoryList", docsubcategoryList);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{dbgyId}")
    @RequiresPermissions("sys:sysdocumentdocsubcategory:info")
    public R info(@PathVariable("dbgyId") Long dbgyId) {

        SysDocumentDocsubcategoryEntity sysDocumentDocsubcategory = sysDocumentDocsubcategoryService.getById(dbgyId);

        return R.ok().put("sysDocumentDocsubcategory", sysDocumentDocsubcategory);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:sysdocumentdocsubcategory:save")
    public R save(@RequestBody SysDocumentDocsubcategoryEntity sysDocumentDocsubcategory) {
        verifyForm(sysDocumentDocsubcategory);
        sysDocumentDocsubcategoryService.save(sysDocumentDocsubcategory);
        //添加所以人查看权限
        if (sysDocumentDocsubcategory.getType() != 2) {
            String lds="{\n" +
                    "    \"dbgyId\":\""+sysDocumentDocsubcategory.getDbgyId()+"\",\n" +
                    "    \"sharelevel\":\"1\",\n" +
                    "    \"sharetype\":\"4\",\n" +
                    "    \"foralluser\":\"1\"\n" +
                    "}";
            sysDocumentDirectoryrightsController.DirecToryrigsave(lds);

        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:sysdocumentdocsubcategory:update")
    public R update(@RequestBody SysDocumentDocsubcategoryEntity sysDocumentDocsubcategory) {
        verifyForm(sysDocumentDocsubcategory);


        sysDocumentDocsubcategoryService.updateById(sysDocumentDocsubcategory);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:sysdocumentdocsubcategory:delete")
    public R delete(Long dbgyIds) {

        //通过父类目录查是否存在子目录  或子文件
        List<SysDocumentDocsubcategoryEntity> list = sysDocumentDocsubcategoryService.qeryListParentId(dbgyIds);

        if (list.size() > 0) {
            return R.error("存在子目录或子文件无法删除");
        }
        List<SysDocumentEntity> documentEntityList = sysDocumentService.queryList(Math.toIntExact(dbgyIds));
        System.out.println(list);
        if (documentEntityList.size() > 0) {
            return R.error("目录下存在文档无法删除");
        }

        sysDocumentDocsubcategoryService.removeByIds(Arrays.asList(dbgyIds));

        return R.ok();
    }


    /**
     * 校验参数
     */
    public void verifyForm(SysDocumentDocsubcategoryEntity bcategoryEntity) {

        if (StringUtils.isEmpty(bcategoryEntity.getCategoryname())) {
            throw new RRException("菜单名称不能为空");

        }

        if (bcategoryEntity.getMaincategoryId() == null) {
            throw new RRException("上级菜单不能为空");
        }
        //菜单
        if (bcategoryEntity.getType() == Constant.MenuType.MENU.getValue()) {
            if (bcategoryEntity.getUrl() == null) {
                throw new RRException("菜单地址不能为空");
            }
        }
        //获取上级菜单类型
        int parentType = Constant.MenuType.CATALOG.getValue();
        if (bcategoryEntity.getMaincategoryId() != 0) {
            SysDocumentDocsubcategoryEntity byId = sysDocumentDocsubcategoryService.getById(bcategoryEntity.getMaincategoryId());
            parentType = byId.getType();

        }


        //目录、菜单
        if (bcategoryEntity.getType() == Constant.MenuType.MENU.getValue() || bcategoryEntity.getType() == Constant.MenuType.CATALOG.getValue()) {
            //目录 菜单 的上级都只能是 目录类型
            if (parentType != Constant.MenuType.CATALOG.getValue()) {
                throw new RRException("上级菜单只能为目录类型");
            }

            return;

        }

        //按钮
        if (bcategoryEntity.getType() == Constant.MenuType.BUTTON.getValue()) {

            if (parentType != Constant.MenuType.MENU.getValue()) {
                throw new RRException("上级菜单只能为菜单类型");
            }
            return;
        }

    }

}
